/***********************************************
If you include this file please add to your sma:
	stock d_custom_item_buy(id, item){
		//this is called when player buys item
	}
	
	add TITLE_DEALERS_MENU to yours language file. This will be title of dealers menu
	add ITEMS_MENU to yours language file. This will be title of items menu
*/	
#if defined _dealers_menus_included
	#endinput
#endif

#define _dealers_menus_included


stock d_dealers_menu(id){
	new text[64];
	new len = charsmax(text);
	formatex(text,  len, "/r%L", id, "TITLE_DEALERS_MENU" );
	new menu = menu_create(text, "dealers_menu_handler");
	new info[1];
	
	for(new dealer =0;dealer<MAX_DEALERS;dealer++) {
		if(!gDealerDef[dealer][Di_Registered])
			continue;
		if(playerAvailableDealers[id][dealer] &&   d_get_dealers_items_count( dealer) > 0){
			info[0] = dealer;
			menu_additem(menu, gDealerDef[dealer][Di_Name], info);
		}
	}
	menu_display(id, menu);
}

public _dealer_menu(iPlugin, iParams){
	new id = get_param(1);
	d_dealers_menu(id);
}

public dealers_menu_handler(id, menu, item){
	if(item == MENU_EXIT) {
		menu_destroy(menu);
		return;
	}
	new info[1];
	new acc, call;
	menu_item_getinfo(menu, item, acc, info, 1, _, _ , call);
	menu_destroy(menu);
	d_dealer_item_menu(id, info[0]);
	return;
}

stock d_dealer_item_menu(id, dealer){
	playerDealer[id] = dealer;
	new text[64];
	new len = charsmax(text);
	formatex(text,  len, "/r%s", gDealerDef[dealer][Di_Name]);
	new menu = menu_create(text, "dealer_item_menu_handler");
	new info[1];
	
	for(new item =0;item<ITEMS;item++) {
		if(playerAvailableItems[id][item]){
			text[0] = '^0';
			formatex(text, len, "%L [%L: %d] %L: %d$",id, ITEM_NAMES[item], id, "COUNT", gDealersItems[dealer][item][Dii_Count], id, "COST", gDealersItems[dealer][item][Dii_Cost]);
			info[0] = item;
			menu_additem(menu, text, info);
		}
	}
	menu_display(id, menu);	
}

public dealer_item_menu_handler(id, menu, item){
	if(item == MENU_EXIT) {
		menu_destroy(menu);
		d_dealers_menu(id);
		return;
	}
	new info[1];
	new acc, call;
	menu_item_getinfo(menu, item, acc, info, 1, _, _ , call);
	menu_destroy(menu);
	new itm = info[0];
	
	d_give_item_from_dealer(id, itm, playerDealer[id]);
	
	d_dealer_item_menu(id, playerDealer[id]);
	return;
}


//{ Item menu
public _item_menu(iPlugin, iParams){
	new id=get_param(1);
	d_item_menu(id);
}


stock d_item_menu(id){
	new text[64];
	new len = charsmax(text);
	formatex(text, len, "/r%L", id, "ITEMS_MENU");
	new menu = menu_create(text, "item_menu_handler");
	new info[1];
	for(new item=0;item<ITEMS;item++){
		if(playerAvailableItems[id][item] && d_anyDealerHaveItem(id, item)){
			text[0] = '^0';
			formatex(text, len, "%L", id, ITEM_NAMES[item]);
			info[0] = item;
			menu_additem(menu, text, info);
		}
	}
	menu_display(id, menu);
}


public item_menu_handler(id, menu, item){
	if(item == MENU_EXIT) {
		menu_destroy(menu);
		return;
	}
	new info[1];
	new acc, call;
	menu_item_getinfo(menu, item, acc, info, 1, _, _ , call);
	menu_destroy(menu);	
	d_item_dealer_menu(id, info[0]);		
}

stock d_item_dealer_menu(id, item){
	new text[64];
	new len = charsmax(text);
	formatex(text, len, "/r%L", id, ITEM_NAMES[item]);
	new menu = menu_create(text, "item_dealer_menu_handler");
	new info[1];
	playerItem[id] = item;
	new count ;
	new cost;
	for(new dealer =0;dealer<MAX_DEALERS;dealer++){
		if(!gDealerDef[dealer][Di_Registered])
			continue;
		count = gDealersItems[dealer][item][Dii_Count];
		cost = gDealersItems[dealer][item][Dii_Cost];
		if(playerAvailableDealers[id][dealer] && count >0) {
			info[0] = dealer;
			text[0] = '^0';
			formatex(text, len, "%s [%L: %d] %L: %d$", gDealerDef[dealer][Di_Name], id, "COUNT", count, id, "COST", cost);
			menu_additem(menu, text, info);
		}
	}
	menu_display(id, menu);
}

public item_dealer_menu_handler(id, menu, item){
	if(item == MENU_EXIT) {
		menu_destroy(menu);
		d_item_menu(id);
		return;
	}
	new info[1];
	new acc, call;
	menu_item_getinfo(menu, item, acc, info, 1, _, _ , call);
	if(d_give_item_from_dealer(id, playerItem[id], info[0], false)){
		d_item_menu(id);
	} else {
		d_item_dealer_menu(id, playerItem[id]);
	}
}
//}
